package com.weiaibenpao.demo.chislim.ui.activity.moment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.FansAttentionListBean;
import com.weiaibenpao.demo.chislim.db.preferences.UserPreferences;
import com.weiaibenpao.demo.chislim.presenter.FansPresenter;
import com.weiaibenpao.demo.chislim.ui.adapter.FansAdapter;
import com.weiaibenpao.demo.chislim.utils.Constant;
import com.weiaibenpao.demo.chislim.view.FansView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AttentionFansActivity extends BaseActivity<FansView, FansPresenter> implements FansView,
        BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener,BaseQuickAdapter.OnItemChildClickListener {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.mid_text)
    TextView midText;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private int userId;
    private FansAdapter fansAdapter;
    private int page;
    /**
     *
     */
    private int type;
    private View view;
    private TextView tvEmpty;

    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_attention_fans;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        type = getIntent().getIntExtra(Constant.TYPE, 0);
        userId =getIntent().getIntExtra(Constant.USER_ID, UserPreferences.getInstance().getInt(UserPreferences.USER_ID));
        initAdapter();
        doRefresh();
    }

    private void initAdapter() {
        view = LayoutInflater.from(this).inflate(R.layout.layout_empty, null);
        tvEmpty = ((TextView) view.findViewById(R.id.tv_empty_new));
        tvEmpty.setText("暂无"+(type==1?"关注":"粉丝"));
        fansAdapter = new FansAdapter();
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        recyclerView.setAdapter(fansAdapter);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setRefreshing(true);
        fansAdapter.setOnLoadMoreListener(this, recyclerView);
        fansAdapter.setOnItemChildClickListener(this);

    }

    /**
     *
     * @param context
     * @param type==0  粉丝  type==1 关注
     *
     */
    public static void startSelf(Context context,int type,int beUserId) {
        Intent intent = new Intent(context, AttentionFansActivity.class);
        intent.putExtra(Constant.TYPE, type);
        intent.putExtra(Constant.USER_ID, beUserId);

        context.startActivity(intent);
    }

    @Override
    public void upAttentionListSuccess(FansAttentionListBean fansAttentionListBean) {
        swipeLayout.setRefreshing(false);
        fansAdapter.setEmptyView(view);
        List<FansAttentionListBean.ListBean> listBeans = fansAttentionListBean.getList();
        boolean isRefresh=page==1;
        setData(listBeans,isRefresh);
    }

    @Override
    public void onFail() {
        swipeLayout.setRefreshing(false);
    }

    private void setData(List<FansAttentionListBean.ListBean> listBeans, boolean isRefresh) {
        int size=listBeans!=null?listBeans.size():0;
        if (page==1){
            fansAdapter.setNewData(listBeans);

        }else {
            fansAdapter.addData(listBeans);
        }
        if (size < Constant.DEFAULT_PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            fansAdapter.loadMoreEnd(isRefresh);
        } else {
            fansAdapter.loadMoreComplete();
        }
    }
    @OnClick(R.id.left_image)
    public void onViewClicked() {
        finish();
    }


    @Override
    public FansPresenter initPresenter() {
        return new FansPresenter();
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        doRequest();
    }

    @Override
    public void onRefresh() {
        doRefresh();
    }

    private void doRefresh() {
        page = 1;
        fansAdapter.setEnableLoadMore(false);
        doRequest();
    }

    private void doRequest() {
       if (type==1){
           mPresenter.getAttentionList(userId, page);
       }else {
           mPresenter.getFansList(userId, page);

       }

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        Logger.e(fansAdapter.getItem(position).getUserId()+"");
        EventBus.getDefault().post(fansAdapter.getItem(position));
        finish();
    }
}
